package com.siddy.bbs.activity;

import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.util.SparseArray;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;

import com.siddy.bbs.R;
import com.siddy.bbs.fragment.SlidingMenuListFragment;
import com.siddy.bbs.fragment.Top10Fragment;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingActivityHelper;

public class FragmentContainerActivity extends FragmentActivity implements SlidingMenuListFragment.Callbacks,
		Top10Fragment.OnFragmentInteractionListener, AdapterView.OnItemClickListener {

	protected SlidingActivityHelper mSlidingHelper;

	private ActionBar supportedActionBar;

	private ListFragment mMenuFragment;

	private int currentFragment = R.string.tab_top10;

	private SparseArray<Fragment> fragments = new SparseArray<Fragment>(5);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mSlidingHelper = new SlidingActivityHelper(this);
		mSlidingHelper.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fragment_container);
		setBehindContentView(R.layout.menu_frame);
		if (savedInstanceState == null) {
			FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
			mMenuFragment = new SlidingMenuListFragment();
			try {
				t.replace(R.id.menu_frame, mMenuFragment);
				t.commit();
			} catch (Exception e) {
				// TODO: handle exception
			}
		} else {
			mMenuFragment = (ListFragment) getSupportFragmentManager().findFragmentById(R.id.menu_frame);
		}
		// mMenuFragment.getListView().setOnItemClickListener(this);

		supportedActionBar = getActionBar();
		supportedActionBar.setDisplayHomeAsUpEnabled(true);
		supportedActionBar.setDisplayShowHomeEnabled(true);

		mSlidingHelper.setSlidingActionBarEnabled(false);

		// Customize
		SlidingMenu sm = getSlidingMenu();
		sm.setShadowWidthRes(R.dimen.shadow_width);
		sm.setShadowDrawable(R.drawable.shadow);
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setFadeDegree(0.35f);

		// TODO: If exposing deep links into your app, handle intents here.
	}

	@Override
	public View findViewById(int id) {
		View v = super.findViewById(id);
		if (v != null)
			return v;
		return mSlidingHelper.findViewById(id);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		mSlidingHelper.onPostCreate(savedInstanceState);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mSlidingHelper.onSaveInstanceState(outState);
	}

	@Override
	public void setContentView(View view, LayoutParams params) {
		super.setContentView(view, params);
		mSlidingHelper.registerAboveContentView(view, params);
	}

	@Override
	public void setContentView(int layoutResId) {
		setContentView(getLayoutInflater().inflate(layoutResId, null));
	}

	@Override
	public void setContentView(View view) {
		setContentView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	}

	public void setBehindContentView(int id) {
		setBehindContentView(getLayoutInflater().inflate(id, null));
	}

	public void setBehindContentView(View v) {
		setBehindContentView(v, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	}

	public void setBehindContentView(View v, LayoutParams params) {
		mSlidingHelper.setBehindContentView(v, params);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		boolean b = mSlidingHelper.onKeyUp(keyCode, event);
		return b ? b : super.onKeyUp(keyCode, event);
	}

	public SlidingMenu getSlidingMenu() {
		return mSlidingHelper.getSlidingMenu();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			mSlidingHelper.toggle();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

	}

	@Override
	public void onMenuItemSelected(int resId) {
		final int id = resId;
		if (currentFragment == id) {
			mSlidingHelper.toggle();
			return;
		}
		switch (id) {
		case R.string.tab_top10: {
			Fragment fragment = fragments.get(id);
			if (fragment == null) {
				fragment = new Top10Fragment();
				fragments.put(id, fragment);
			}
			getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();
			break;
		}
		case R.string.tab_recommend:
		case R.string.tab_board:
		case R.string.tab_favorite:
		case R.string.tab_mail:
		case R.string.tab_setting:
			break;

		default:
			break;
		}
	}

	@Override
	public void onFragmentInteraction(String id) {
		// TODO Auto-generated method stub

	}
}
